<template>
    <div class="main-content clearfix">
        <div class="instructions">修改密码</div>
        <img v-lazy="'./images/portrait1.jpg'" />
        <form action="javascript:">
            <ul class="message">
                <li class="clearfix">
                    <div class="label">ID:</div>
                    <input type="text" class="information ID" :value="userInfo.accountId" disabled="disabled" />
                </li>
                <li class="clearfix">
                    <div class="label">旧密码：</div>
                    <input type="password" class="information old-password" v-model="oldAccountPwd" />
                </li>
                <li class="clearfix">
                    <div class="label">新密码：</div>
                    <input type="password" class="information new-password" v-model="newAccountPwd" />
                </li>
                <li class="clearfix">
                    <div class="label">确认新密码：</div>
                    <input type="password" class="information new-password" v-model="newAccountPwdAgain" />
                </li>
            </ul>
            <button class="button" @click="userModify">提交</button>
        </form>
    </div>
</template>

<script>
import { mapState } from "vuex";
export default {
    data() {
        return {
            oldAccountPwd: "",
            newAccountPwd: "",
            newAccountPwdAgain: "",
        };
    },
    methods: {
        async userModify() {
            // 若是有一个为空则提醒用户填空
            if (
                !(
                    this.oldAccountPwd.trim() &&
                    this.newAccountPwd.trim() &&
                    this.newAccountPwdAgain.trim()
                )
            ) {
                this.$message({
                    message: "内容不能为空",
                    type: "error",
                });
                return;
            }
            if (this.newAccountPwd.trim() !== this.newAccountPwdAgain.trim()) {
                this.$message({
                    message: "两次密码应当一致",
                    type: "error",
                });
                return;
            }
            let accountModify = {
                accountPwd: this.newAccountPwd,
                oldAccountPwd: this.oldAccountPwd,
            };
            try {
                await this.$store.dispatch("user/userModify", accountModify);
                this.$message({
                    message: "修改成功",
                    type: "success",
                });
            } catch (error) {
                this.$message({
                    message: error,
                    type: "error",
                });
            } finally {
                // 无论成功与否，都要清空数据
                this.oldAccountPwd = "";
                this.newAccountPwd = "";
                this.newAccountPwdAgain = "";
            }
        },
    },
    computed: {
        ...mapState("user", ["userInfo"]),
    },
};
</script>

<style scoped>
/*设置修改个人信息栏*/
.main-content {
    margin-top: 20px;
    width: 1040px;
    min-height: 600px;
    background-color: rgba(255, 255, 255, 0.8);
}
/*设置头标*/
.main-content .instructions {
    width: 980px;
    padding-bottom: 20px;
    margin: 30px auto;
    border-bottom: 2px solid rgba(91, 81, 69, 0.4);
}
/*修改头像大小样式*/
.main-content img {
    width: 150px;
    height: 150px;
    border-radius: 25%;
    margin: 80px 150px 0 0;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    float: right;
}
/*修改表单位置*/
.main-content form {
    float: left;
    margin: 100px 0 100px 200px;
    padding: 50px;
    border: 2px solid rgba(91, 81, 69, 0.4);
}

/*修改表单数据的排列*/
.main-content form li {
    font-size: 14px;
    line-height: 30px;
    margin-bottom: 15px;
}

.main-content form li .label,
.main-content form li .information {
    float: left;
}
/*修改label的样式让输入框对齐*/
.main-content form li .label {
    width: 90px;
    margin-top: 3px;
}

/*修改input组件的样式*/
.main-content form li .information {
    width: 240px;
    height: 30px;
    color: rgba(91, 81, 69, 0.8);
    font-size: 15px;
    line-height: 30px;
    border: none;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    outline: none;
    background-color: transparent;
}

.main-content form .information:hover {
    border-color: rgb(193, 193, 193);
}

.main-content form .information:focus {
    color: rgb(91, 81, 69);
    border-color: rgb(91, 81, 69);
}

.main-content form .button {
    border: none;
    width: 100px;
    height: 40px;
    display: block;
    background-image: linear-gradient(
        to top,
        rgb(72, 62, 50),
        rgb(172, 180, 178)
    );
    color: white;
    font-size: 16px;
    line-height: 30px;
    margin: 20px auto 0;
    box-shadow: 0 0 5px rgba(72, 62, 50, 0.3);
}

.main-content form .button:hover {
    background-image: linear-gradient(
        to top,
        rgb(96, 82, 70),
        rgb(192, 203, 200)
    );
}

.main-content form .button:active {
    background-image: linear-gradient(
        to top,
        rgb(85, 77, 60),
        rgb(182, 185, 190)
    );
}
</style>